package listener

import (
	"mks/message"
	"net"
)

/*
listen 是指没有鼠标键盘的设备端
*/

func StartClientServer() {
	//开启一个UDP服务器
	addr, e := net.ResolveUDPAddr("udp", ":8888")
	if e != nil {
		panic(e)
	}
	conn, e := net.ListenUDP("udp", addr)
	defer conn.Close()
	if e != nil {
		panic(e)
	}
	println("启动成功", addr.String())
	for {
		//接收数据
		buf := make([]byte, 512)
		n, _, e := conn.ReadFromUDP(buf)
		if e != nil {
			panic(e)
		}
		//打印数据
		println("len=", n, "content=", string(buf[:n]))
		//发送数据
		//conn.WriteToUDP(buf[:n], addrFrom)
		HandleMouseMessage(message.NewMessageFromFields(buf[:n]))

	}
}
